<template>
  <div class="app-container">
    <div class="table_content">
      <div class="btn-btm" v-if="perms==0">
        <el-button type="primary" size="mini" plain icon="el-icon-plus" @click="handleAdd">添加</el-button>
        <el-button type="success" size="mini" plain icon="el-icon-view" @click="handleSee">查看法规目录
        </el-button>
      </div>

      <!--表格渲染-->
      <el-table ref="table" v-loading="loading" :data="list" style="width: 100%;" border
                :header-cell-style="{background: '#F8F8F9',padding: '6px 0 !important',color: '#515a6e'}">
        <el-table-column prop="title" fixed="left" label="标题" />
        <el-table-column prop="policyType" label="所属分类" />
        <el-table-column prop="content" label="政策法规内容" />
        <el-table-column prop="unit" label="发布单位" />
        <el-table-column prop="datePub" label="发布日期" />
        <el-table-column fixed="right" label="操作" min-width="100" v-if="perms==0">
          <template slot-scope="scope">
            <el-button class="btn" type="text" size="small"
                       @click="handleEdit(scope.$index,scope.row)">编辑</el-button>
            <el-popover
              placement="top"
              width="200"
              trigger="manual"
              :ref="'popover-' + scope.row.orgID">
              <p>确定删除该数据吗？</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="$refs[`popover-` + scope.row.orgID].doClose()">取消</el-button>
                <el-button type="primary" size="mini" @click="handleDel(scope.$index,scope.row)">确定</el-button>
              </div>
              <el-button class="btn" type="text" size="small" slot="reference"
                         @click="$refs[`popover-` + scope.row.orgID].doShow()">删除</el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <pagination v-show="total>0" :total="total"
                  :page.sync="listQuery.pageNum"
                  :limit.sync="listQuery.pageSize"
                  @pagination="getList" />

      <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
        <el-form :model="editForm" label-width="120px" :rules="editFormRules" ref="editForm">
          <el-form-item label="标题" prop="title" class="form-item">
            <el-input v-model="editForm.title" placeholder="标题" class="filter-item"></el-input>
          </el-form-item>
          <el-form-item label="所属分类" prop="policyType" class="form-item">
            <el-select placeholder="请选择" style="width:100%;" clearable v-model="editForm.userType"
                       class="form-select" :popper-append-to-body="false" popper-class="select-popper">
              <el-option v-for="item in optionType" :value="item.policyType" :label="item.policyType"></el-option>
            </el-select>
            <!--<el-input v-model="editForm.policyType" placeholder="所属分类" class="filter-item"></el-input>-->
          </el-form-item>
          <el-form-item label="发布单位" prop="unit" class="form-item">
            <el-input v-model="editForm.unit" placeholder="发布单位" class="filter-item"></el-input>
          </el-form-item>
          <el-form-item label="政策法规内容" prop="content" class="form-item">
            <quill-editor ref="child" :values="editForm.content"></quill-editor>
            <!--<el-input v-model="editForm.content" placeholder="政策法规内容" class="filter-item"></el-input>-->
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="mini" @click.native="dialogFormVisible=false">取消</el-button>
          <el-button type="primary" size="mini" @click="createData">{{dialogStatus == 'create'?'添加':'编辑'}}</el-button>
        </div>
      </el-dialog>

      <el-dialog title="政策法规" :visible.sync="seeFormVisible" :close-on-click-modal="false">
        <el-form :model="seeForm" :inline="true" label-width="100px" ref="seeForm">
          <el-form-item label="法规标题" prop="policyType" :rules="{required: true,message: '法规标题不能为空', trigger: 'blur'}" class="form-item">
            <el-input v-model="seeForm.policyType" placeholder="法规标题" class="filter-item"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="success" size="mini" icon="el-icon-plus" @click="handleSeeAdd">添加</el-button>
          </el-form-item>
        </el-form>
        <el-table ref="table" v-loading="seeLoading" :data="seeList" style="width: 100%;" border
                  :header-cell-style="{background: '#F8F8F9',padding: '6px 0 !important',color: '#515a6e'}">
          <el-table-column prop="policyType" label="分类目录" />
          <el-table-column fixed="right" label="操作" min-width="100">
            <template slot-scope="scope">
              <el-popover
                placement="top"
                width="200"
                trigger="manual"
                :ref="'popover-' + scope.row.policyType">
                <p>确定删除该数据吗？</p>
                <div style="text-align: right; margin: 0">
                  <el-button size="mini" type="text" @click="$refs[`popover-` + scope.row.policyType].doClose()">取消</el-button>
                  <el-button type="primary" size="mini" @click="handleSeeDel(scope.$index,scope.row)">确定</el-button>
                </div>
                <el-button class="btn" type="text" size="small" slot="reference"
                           @click="$refs[`popover-` + scope.row.policyType].doShow()">删除</el-button>
              </el-popover>
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import { doPost, doGet, doFile } from '@/api/index'
  import Pagination from '@/components/Pagination/index'
  import quillEditor from '@/components/quillEditor/index'
  import { mapGetters } from 'vuex'

  export default {
    name: "policy",
    components: {Pagination, quillEditor},
    computed: {
      ...mapGetters([
        'sidebar',
        'avatar',
        'name',
        'perms',
        'fistFlag',
        'userForm'
      ])
    },
    data() {
      return {
        loading: false,
        listQuery: {
          pageNum: 1,
          pageSize: 20
        },
        total: 0,
        list: [],
        dialogStatus: '',
        textMap:{
          update: '编辑',
          create: '新增'
        },
        dialogFormVisible: false,
        editForm: {
          title: '',
          policyType: '',
          datePub:'',
          unit:'',
          content:''
        },
        editFormRules: {},
        optionType: [],
        seeFormVisible: false,
        seeLoading: false,
        seeList: [],
        seeForm: {
          policyType: ''
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      getList() {
        this.loading = true
        doPost('policy/page',this.listQuery).then(res => {
          this.loading = false
          this.list = res.data.list
          this.total = res.data.total
        }).catch(error => {
          this.list = []
          this.total = 0
          this.loading = false
        })
      },
      handleAdd() {
        this.dialogFormVisible = true
        this.dialogStatus = 'create'
        this.editForm = {
          title: '',
          policyType: '',
          datePub:'',
          unit:'',
          content:''
        }
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
      },
      //编辑弹窗
      handleEdit(index, row) {
        if (row) {
          for (var key in row) {
            if (row[key] == null || typeof(row[key]) === "undefined" || row[key] == undefined) {
              row[key] = ''
            }
          }
        }
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.editForm = Object.assign({}, row)
        this.$nextTick(() => {
          this.$refs['editForm'].clearValidate()
        })
      },
      //提交表单
      createData(){
        this.$refs.editForm.validate(valid => {
          if (valid) {
            doPost(`menu/${this.dialogStatus == 'create' ? 'add' : 'update'}`, this.editForm).then(res => {
              if (res.code == 200) {
                this.getList()
                this.$message.success({
                  message: this.dialogStatus == 'create'?'添加成功':'修改成功',
                  type: 'success'
                })
                this.dialogFormVisible = false
              } else {
                this.$message.error(res.message)
              }
            }).catch(error => {
              this.$message.error(error.message)
            })
          }
        })
      },
      handleSee() {
        this.seeFormVisible = true
        this.seeGetList()
      },
      seeGetList() {
        this.seeLoading = true
        doPost('policy/type/list').then(res => {
          this.seeLoading = false
          this.seeList = res.data
        }).catch(error => {
          this.seeList = []
          this.seeLoading = false
        })
      },
      //添加目录
      handleSeeAdd() {
        this.$refs.seeForm.validate(valid => {
          if (valid) {
            doPost('policy/type/add',this.seeForm).then(res => {
              if (res.code==200){
                this.seeGetList()
                this.$message.success({
                  message: '添加成功',
                  type: 'success'
                })
                this.dialogFormVisible = false
              } else {
                this.$message.error(error.message)
              }
            }).catch(error => {
              this.$message.error(error.message)
            })
          }
        })
      },
      //删除目录
      handleSeeDel(index, row){
        doPost('policy/type/delete/' + row.policyType).then(res => {
          if (res.code == 200) {
            this.$message.success({
              type: 'success',
              message: '删除成功'
            })
            this.seeList.splice(index, 1)
            this.$refs[`popover-` + row.policyType].doClose()
          } else {
            this.$refs[`popover-` + row.policyType].doClose()
            this.$message.error(res.message)
          }
        }).catch(error => {
          this.$refs[`popover-` + row.policyType].doClose()
          this.$message.error(error.message)
        })
      }
    }
  }
</script>

<style scoped>

</style>
